﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using YArchitech.LIB;
using YArchitech.MEP.Electric.Common;
using YArchitech.Revit.Electric.Common;

namespace HYRElectric.Equipment
{
	public partial class LeadSymbolForm : System.Windows.Forms.Form
	{
		public bool IsClick { get; set; }

		public DbFamilyObject SelFamily { get; set; }

		public LeadSymbolForm(List<DbFamilyObject> dbFamilyObjects, Document doc)
		{
			this.m_Doc = doc;
			this.InitializeComponent();
			this.IsClick = false;
			//base.Icon = Resource.YArchitechIcon;
			this.InitialFamilyImages(dbFamilyObjects);
		}

		private void InitialFamilyImages(List<DbFamilyObject> dbFamilyObjects)
		{
			List<Button> list = new List<Button>();
			foreach (object obj in base.Controls)
			{
				Button button = obj as Button;
				if (button != null)
				{
					list.Add(button);
				}
			}
			for (int i = 0; i < list.Count - 1; i++)
			{
				for (int j = 0; j < list.Count - 1 - i; j++)
				{
					string name = list[j].Name;
					string name2 = list[j + 1].Name;
					if (Convert.ToInt32(name.Substring(name.Length - 1)) > Convert.ToInt32(name2.Substring(name2.Length - 1)))
					{
						Button value = list[j];
						list[j] = list[j + 1];
						list[j + 1] = value;
					}
				}
			}
			for (int k = 0; k < 8; k++)
			{
				list[k].Image = Image.FromFile(dbFamilyObjects[k].Image2D);
				list[k].Tag = dbFamilyObjects[k];
			}
		}

		private void LeadSymbolForm_Load(object sender, EventArgs e)
		{
			this.xmlHelper.LoadXml(this.path, "Root");
			this.InitialButton();
		}

		private void InitialButton()
		{
			this.xmlHelper.SelectNode(Path.Combine("Root", "HYLeadSymbol", "Params"));
			string attribute = this.xmlHelper.GetAttribute("X");
			string attribute2 = this.xmlHelper.GetAttribute("Y");
			if (!string.IsNullOrEmpty(attribute) && !string.IsNullOrEmpty(attribute2))
			{
                base.Location = new System.Drawing.Point(Convert.ToInt32(attribute), Convert.ToInt32(attribute2));
			}
			string text = this.xmlHelper.GetAttribute("SelLeadSymbol");
			if (string.IsNullOrEmpty(text))
			{
				text = "btnPic1";
			}
			Button button = null;
			uint num = 0;//<PrivateImplementationDetails>.ComputeStringHash(text);
			if (num <= 663205481u)
			{
				if (num <= 629650243u)
				{
					if (num != 612872624u)
					{
						if (num == 629650243u)
						{
							if (text == "btnPic4")
							{
								button = this.btnPic4;
							}
						}
					}
					else if (text == "btnPic5")
					{
						button = this.btnPic5;
					}
				}
				else if (num != 646427862u)
				{
					if (num == 663205481u)
					{
						if (text == "btnPic6")
						{
							button = this.btnPic6;
						}
					}
				}
				else if (text == "btnPic7")
				{
					button = this.btnPic7;
				}
			}
			else if (num <= 713538338u)
			{
				if (num != 679983100u)
				{
					if (num == 713538338u)
					{
						if (text == "btnPic3")
						{
							button = this.btnPic3;
						}
					}
				}
				else if (text == "btnPic1")
				{
					button = this.btnPic1;
				}
			}
			else if (num != 730315957u)
			{
				if (num == 830981671u)
				{
					if (text == "btnPic8")
					{
						button = this.btnPic8;
					}
				}
			}
			else if (text == "btnPic2")
			{
				button = this.btnPic2;
			}
			if (button != null)
			{
				this.ChangingButtonState(button);
			}
			this.m_ElementsCount = this.GetElementCount(this.m_Doc);
		}

		private void ChangingButtonState(Button sender)
		{
            System.Drawing.Color backColor = this.BackColor;
			if (sender == null)
			{
				return;
			}
			string name = sender.Name;
			uint num = 0;//<PrivateImplementationDetails>.ComputeStringHash(name);
			if (num <= 663205481u)
			{
				if (num <= 629650243u)
				{
					if (num != 612872624u)
					{
						if (num == 629650243u)
						{
							if (name == "btnPic4")
							{
								this.SelFamily = (DbFamilyObject)this.btnPic4.Tag;
							}
						}
					}
					else if (name == "btnPic5")
					{
						this.SelFamily = (DbFamilyObject)this.btnPic5.Tag;
					}
				}
				else if (num != 646427862u)
				{
					if (num == 663205481u)
					{
						if (name == "btnPic6")
						{
							this.SelFamily = (DbFamilyObject)this.btnPic6.Tag;
						}
					}
				}
				else if (name == "btnPic7")
				{
					this.SelFamily = (DbFamilyObject)this.btnPic7.Tag;
				}
			}
			else if (num <= 713538338u)
			{
				if (num != 679983100u)
				{
					if (num == 713538338u)
					{
						if (name == "btnPic3")
						{
							this.SelFamily = (DbFamilyObject)this.btnPic3.Tag;
						}
					}
				}
				else if (name == "btnPic1")
				{
					this.SelFamily = (DbFamilyObject)this.btnPic1.Tag;
				}
			}
			else if (num != 730315957u)
			{
				if (num == 830981671u)
				{
					if (name == "btnPic8")
					{
						this.SelFamily = (DbFamilyObject)this.btnPic8.Tag;
					}
				}
			}
			else if (name == "btnPic2")
			{
				this.SelFamily = (DbFamilyObject)this.btnPic2.Tag;
			}
			foreach (object obj in base.Controls)
			{
				Button button = obj as Button;
				if (button != null)
				{
					button.BackColor = backColor;
					if (button == sender)
					{
                        button.BackColor = System.Drawing.Color.LightSkyBlue;
					}
				}
			}
			sender.Select();
			this.toolTip1.SetToolTip(sender, ((DbFamilyObject)sender.Tag).Name);
		}

		private void btnPic1_Click(object sender, EventArgs e)
		{
			int elementCount = this.GetElementCount(this.m_Doc);
			this.ChangingButtonState(sender as Button);
			this.IsClick = true;
			int wMsg = 7;
			LeadSymbolForm.SendMessage(WindowHelper.HWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
			this.PressESC();
			if (elementCount > this.m_ElementsCount)
			{
				this.m_ElementsCount = elementCount;
				LeadSymbolForm.SendMessage(WindowHelper.HWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
				this.PressESC();
			}
		}

		private void PressESC()
		{
			int bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			LeadSymbolForm.keybd_event((byte)bVk, 0, dwFlags, 0);
			LeadSymbolForm.keybd_event((byte)bVk, 0, dwFlags2, 0);
		}

		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		private void LeadSymbolForm_FormClosed(object sender, FormClosedEventArgs e)
		{
			this.PressESC();
		}

		private void LeadSymbolForm_MouseLeave(object sender, EventArgs e)
		{
			if (!base.ClientRectangle.Contains(Cursor.Position))
			{
				int wMsg = 7;
				LeadSymbolForm.SendMessage(WindowHelper.HWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
				return;
			}
			base.Focus();
		}

		private void btnPic1_MouseHover(object sender, EventArgs e)
		{
			base.Activate();
			Button button = sender as Button;
			this.toolTip1.SetToolTip(button, ((DbFamilyObject)button.Tag).Name);
		}

		private void LeadSymbolForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			this.xmlHelper.SelectNode(Path.Combine("Root", "HYLeadSymbol"));
			this.xmlHelper.SelectNode(Path.Combine("Root", "HYLeadSymbol", "Params"));
			foreach (object obj in base.Controls)
			{
				Button button = obj as Button;
                if (button != null && button.BackColor == System.Drawing.Color.LightSkyBlue)
				{
					this.xmlHelper.SetAttribute("SelLeadSymbol", button.Name);
					break;
				}
			}
			this.xmlHelper.SetAttribute("X", base.Location.X.ToString());
			this.xmlHelper.SetAttribute("Y", base.Location.Y.ToString());
			this.xmlHelper.SaveXml("");
		}

		private int GetElementCount(Document document)
		{
			return this.GetElements(document).ToElements().Count;
		}

		private FilteredElementCollector GetElements(Document document)
		{
			return new FilteredElementCollector(document, document.ActiveView.Id).OfCategory(BuiltInCategory.OST_GenericAnnotation).OfClass(typeof(FamilyInstance));
		}

		private Document m_Doc;

		private YArchitech.MEP.Electric.Common.XmlHelper xmlHelper = new YArchitech.MEP.Electric.Common.XmlHelper();

		private string path = Path.Combine(new string[]
		{
			Product.WorkingLocation + "LeadSymbol.xml"
		});

		private const string Root = "Root";

		private const string Node = "HYLeadSymbol";

		private const string ChildNode = "Params";

		private const string LocationX = "X";

		private const string LocationY = "Y";

		private const string SelLeadSymbol = "SelLeadSymbol";

		private int m_ElementsCount;
	}
}
